//
//  ShoppingViewController.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-10.
//
//

#import "ShoppingViewController.h"
#import "ShoppingGoodsViewController.h"
#import "ShoppingManager.h"
#import "UIImageView+OnlineImage.h"
#import "NetworkAlertView.h"

@interface ShoppingViewController ()

@end

@implementation ShoppingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
     [self.view setBackgroundColor:BackgroundColor];
    [self initTopBar];
    [self createTableView];
    
    [[ShoppingManager shareManager] setDelegate:self];
    if (!self.categoryArray || [self.categoryArray count] == 0)
    {
        [[NetworkAlertView shareView] showNetView];
        [[ShoppingManager shareManager] getShoppingCategory];
    }
}

- (void)dealloc
{
    [[ShoppingManager shareManager] setDelegate:nil];
    RelaseAndNil(_tableView);
    self.categoryArray = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initTopBar
{
    if ([self.navigationItem.title length] == 0) {
        [self.navigationItem setTitle:@"购物商城"];
    }
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], self.view.frame.size.height - 50) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
}

#pragma mark tableView datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.categoryArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSString *reuseIdentifier = @"shopCategoryCell";
//    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//    
//    if (!cell)
//    {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
        //加载图像
    UIImageView *imgview  = [[[UIImageView alloc]init] autorelease];
        imgview.frame = CGRectMake(10, 10, 60, 60);
        [cell addSubview:imgview];
        
        //加载名字
        UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 150, 30)]autorelease];
        namelbl.font = [UIFont boldSystemFontOfSize:20];
    namelbl.textColor = [UIColor darkGrayColor];
        namelbl.adjustsFontSizeToFitWidth = YES;
        [cell addSubview:namelbl];
        
        //加载描述
        UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 45, 150, 20)]autorelease];
        desclbl.font = [UIFont boldSystemFontOfSize:15];
        desclbl.textColor = [UIColor grayColor];
        desclbl.lineBreakMode = UILineBreakModeWordWrap;
        desclbl.numberOfLines = 0;
        [cell addSubview:desclbl];
        
//    }
    
    ShoppingCategory *category = [self.categoryArray objectAtIndex:indexPath.row];
    if (category)
    {
        namelbl.text = category.name;
        desclbl.text = @"文体用品、生活书籍";
        [imgview setImage:[UIImage imageNamed:@"购物商城"]];
    }

    
    return cell;
}

#pragma mark tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    ShoppingGoodsViewController *goodsCtrl = [[ShoppingGoodsViewController alloc] init];
//    goodsCtrl.navigationItem.title = @"文化娱乐";
//    [self.navigationController pushViewController:goodsCtrl animated:YES];
//    [goodsCtrl release];
    ShoppingCategory *selectedCategory = [self.categoryArray objectAtIndex:indexPath.row];
    NSMutableArray *subCategoryArrays = selectedCategory.childCategorys;
    if (subCategoryArrays && [subCategoryArrays count] > 0)
    {
        ShoppingViewController *subCategoryView = [[ShoppingViewController alloc] init];
        subCategoryView.categoryArray = subCategoryArrays;
        [subCategoryView.navigationItem setTitle:selectedCategory.name];
        [self.navigationController pushViewController:subCategoryView animated:YES];
        [subCategoryView release];
    }
    else
    {
        ShoppingGoodsViewController *subGoodsView = [[ShoppingGoodsViewController alloc] init];
        subGoodsView.categoryId = selectedCategory.categoryId;
        [subGoodsView.navigationItem setTitle:selectedCategory.name];
        [self.navigationController pushViewController:subGoodsView animated:YES];
        [subGoodsView release];
    }

}

#pragma mark ShoppingManagerDelegate

- (void)onGetShoppingCategoryFinish:(BOOL)isSuccess
                          Categorys:(NSMutableArray*)categorys
{
    [[NetworkAlertView shareView] delView];
    self.categoryArray = categorys;
    [_tableView reloadData];
}

@end
